package com.cloudcc.mobile.adapter;

import java.util.List;

import android.content.Context;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cloudcc.cloudframe.ui.OnItemClickListener;
import com.cloudcc.cloudframe.util.NumberUtils;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.adapter.BeauRelationAdapter.MyViewHolder;
import com.cloudcc.mobile.entity.beau.BeauInfoEntity.ObjRelation;

public class BeauRelationAdapter extends Adapter<MyViewHolder> {

	private List<ObjRelation> rels;

	private LayoutInflater mLayoutInflater;
	private Context context;
	private OnItemClickListener mOnItemClickListener;

	public BeauRelationAdapter(List<ObjRelation> rels, Context context) {
		super();
		this.rels = rels;
		this.context = context;
		mLayoutInflater = LayoutInflater.from(context);
	}

	@Override
	public int getItemCount() {
		return rels.size();
	}

	@Override
	public void onBindViewHolder(MyViewHolder holder, final int arg1) {
		ObjRelation objRelation = rels.get(arg1);
		if (StringUtils.equals("abe", objRelation.prefix)) {
			boolean blank = StringUtils.isBlank(objRelation.relationSize);
			int int1 = NumberUtils.toInt(objRelation.relationSize, 0);
			holder.tv1.setText((int1>0||blank)?"1":"0");
		}else{
			holder.tv1.setText(objRelation.relationSize);
		}
		holder.tv2.setText(objRelation.relationName);
		holder.convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mOnItemClickListener != null) {
					mOnItemClickListener.onItemClick(arg1);
				}
			}
		});
	}

	public void setOnItemClickListener(OnItemClickListener l) {
		this.mOnItemClickListener = l;
	}

	@Override
	public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
		View view = mLayoutInflater.inflate(R.layout.beau_info_relation_item,
				null, false);
		MyViewHolder holder = new MyViewHolder(view);
		return holder;
	}

	public class MyViewHolder extends ViewHolder {

		TextView tv1;
		TextView tv2;
		View iv1;
		View convertView;

		public MyViewHolder(View view) {
			super(view);
			tv1 = (TextView) view.findViewById(R.id.tv1);
			tv2 = (TextView) view.findViewById(R.id.tv2);
			iv1 = (View) view.findViewById(R.id.iv1);
			convertView = view;
		}

	}

}
